﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XmWinKit.Components
{
    /// <summary>
    /// DialogWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DialogWindow : WindowSkin
    {
        public static readonly DependencyProperty MinimizeVisibilityProperty =
            DependencyProperty.Register("MinimizeVisibility", typeof(Visibility), typeof(DialogWindow), 
                new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty MaximizeVisibilityProperty =
            DependencyProperty.Register("MaximizeVisibility", typeof(Visibility), typeof(DialogWindow), 
                new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(DialogWindow),
                new PropertyMetadata(null));

        public Visibility MinimizeVisibility
        {
            get => (Visibility)GetValue(MinimizeVisibilityProperty);
            set => SetValue(MinimizeVisibilityProperty, value);
        }

        public Visibility MaximizeVisibility
        {
            get => (Visibility)GetValue(MaximizeVisibilityProperty);
            set => SetValue(MaximizeVisibilityProperty, value);
        }

        public object Content
        {
            get => GetValue(ContentProperty);
            set => SetValue(ContentProperty, value);
        }

        public DialogWindow()
        {
            InitializeComponent();
        }

         private void OnMinimizeButtonClick(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void OnMaximizeButtonClick(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
            else
            {
                WindowState = WindowState.Maximized;
            }
        }

        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void OnTitleBarMouseDown(object sender, MouseButtonEventArgs e)
        {

            if (this.MaximizeVisibility != Visibility.Visible) return;

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (e.ClickCount == 2)
                {
                    // 双击处理
                    if (this.WindowState == WindowState.Maximized)
                    {
                        this.WindowState = WindowState.Normal;
                    }
                    else
                    {
                        this.WindowState = WindowState.Maximized;
                    }
                    e.Handled = true;  // 标记事件已处理，防止触发 DragMove
                }
                else
                {
                    // 单击处理
                    this.DragMove();
                }
            }
        }

        private void OnTitleBarDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
        }
    }
}
